<template>
  <div class="login-form">
    <h1>Login SiriusAdmin</h1>
    <el-form
      :model="loginForm"
      status-icon :rules="loginRules"
      ref="loginForm"
      label-width="0px"
      class="demo-ruleForm"
    >
      <el-form-item prop="userId">
        <el-input @keyup.enter.native="submitForm('loginForm')" type="text" v-model="loginForm.userId" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item prop="userPasswd">
        <el-input @keyup.enter.native="submitForm('loginForm')" type="password" v-model="loginForm.userPasswd" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="input-width" type="primary" @click="submitForm('loginForm')">登 录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        userId: '',
        userName: 'SiriusName', // 测试用
        userPasswd: ''
      },
      loginRules: {
        userId: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        userPasswd: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let _this = this
          this.$http.post('/login', this.loginForm)
            .then(function (response) {
              if (response.success) {
                _this.$router.push('/')
              } else {
                _this.$alert('登录失败，' + response.data.resultMessage, '提示信息', {
                  confirmButtonText: '确定',
                  type: 'error'
                })
              }
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style>
  .login-form {
    position: relative;
    top: 200px;
    margin: auto;
    text-align: center;
  }
  .input-width, .login-form {
    width: 360px;
  }
</style>
